package com.alan.camunda.boot.groovymod.language;

import com.alan.camunda.boot.util.GroovyUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class LanguageMath {

    private static GroovyUtil groovyUtil;



    public static void main(String[] args) {

        Map<String, Object> variables = new HashMap<>();
        variables.put("De_0_1", new BigDecimal("0.1"));
        variables.put("De_0_2", new BigDecimal("0.2"));
        variables.put("Do_0_1", Double.valueOf("0.1"));
        variables.put("Do_0_2", Double.valueOf("0.2"));
        variables.put("Int_1", Integer.valueOf("1"));
        variables.put("Int_3", Integer.valueOf("3"));

        groovyUtil = new GroovyUtil(variables);

        log.info("in Java language: 1 / 3 = {}", 1/3);
        log.info("in Java language: 0.1 + 0.2 = {}", 0.1 + 0.2);

        log.info("参数为 {}", JSON.toJSONString(variables));
        log.info("De_ 代表 BigDecimal类型变量，Do_ 代表 Double 类型变量");


        execute("De_0_1 + De_0_2");
        execute("De_0_1 + 0.2");
        execute("De_0_1 + Do_0_2");
        execute("De_0_1 + 1");
        execute("Do_0_1 + 1");
        execute("1 + 2");
        execute("1 + 2L");
        execute("Int_1 + 2");
        execute("Int_1 / 3");
        execute("Int_1 / Int_3");
        execute("Int_3 / Int_1");
        execute("Int_3 * 0.1");
        execute("Int_3 * Do_0_1");
        execute("9223372036854765808");

        String jsonStr = """
                {
                  "price": 19.50,
                  "order": 2,
                  "ts": 9223372036854765808
                }
                """;
        System.out.println(JSONObject.parse(jsonStr).get("price").getClass());  // BigDecimal
        System.out.println(JSONObject.parse(jsonStr).get("order").getClass());  // Integer
        System.out.println(JSONObject.parse(jsonStr).get("ts").getClass());     // Long
    }



    private static void execute(String expression) {
        log.info("{}  ----> {} ({})", expression, groovyUtil.getValue(expression), groovyUtil.getValue(expression).getClass());
    }



}
